﻿using System;

namespace CSharp入门实践项目
{
    class Program
    {
        static void Main(string[] args)
        {
            #region 控制台基础设置
            //设置光标隐藏
            Console.CursorVisible = false;
            //设置游戏窗口大小
            //控制台中一个单位高度等于两个单位宽度
            int width = 60;
            int height = 30;    //此处设置需小于等于43，否则会出错，控制台高度以行而不是像素为单位指定，超过43则会超过显示器范围
            Console.SetWindowSize(width, height);
            Console.SetBufferSize(width, height);       //此处禁用诊断CA1416
            #endregion

            #region 实现多场景切换
            //实现多场景切换
            int nowSceneID = 1;     //此处禁用诊断CS0219

            while (true)
            {
                switch (nowSceneID)
                {
                    //开始场景实现
                    case 1:
                        Console.Clear();
                        #region 开始场景实现
                        Console.SetCursorPosition(width / 2 - 4, 5);        //实现标题居中（汉字占两个x轴单位）
                        Console.WriteLine("营救公主");

                        int nowSelIndex = 0;
                        while (true)
                        {
                            //定义一个标识 isQuitWhile 用于处理在while中的switch中想要使用break直接退出while循环的情况
                            bool isQuitWhile = false;
                            //根据nowSelIndex的值选择相应文字颜色
                            Console.SetCursorPosition(width / 2 - 4, 9);
                            Console.ForegroundColor = nowSelIndex == 0 ? ConsoleColor.Red : ConsoleColor.White;
                            Console.WriteLine("开始游戏");

                            Console.SetCursorPosition(width / 2 - 4, 11);
                            Console.ForegroundColor = nowSelIndex == 1 ? ConsoleColor.Red : ConsoleColor.White;
                            Console.WriteLine("结束游戏");

                            //实现输入WS切换 开始 与 结束 选项 ，输入J确定选项功能
                            char c = Console.ReadKey(true).KeyChar;
                            switch (c)
                            {
                                case 'w':
                                case 'W':
                                    --nowSelIndex;
                                    if (nowSelIndex < 0)
                                    {
                                        nowSelIndex = 1;
                                    }
                                    break;
                                case 's':
                                case 'S':
                                    ++nowSelIndex;
                                    if (nowSelIndex > 1)
                                    {
                                        nowSelIndex = 0;
                                    }
                                    break;
                                case 'j':
                                case 'J':
                                    if (nowSelIndex == 0)
                                    {
                                        nowSceneID = 2;
                                        isQuitWhile = true;
                                        break;
                                    }
                                    else
                                    {
                                        Environment.Exit(0);
                                    }
                                    break;
                            }
                            if (isQuitWhile)
                            {
                                break;
                            }
                        }
                        #endregion
                        break;

                    case 2:
                        Console.Clear();
                        #region 游戏场景实现

                        #region 绘制游戏场景边界
                        //设置颜色
                        Console.ForegroundColor = ConsoleColor.Red;
                        //绘制x轴方向边界
                        for (int i = 0; i < width; i += 2)
                        {
                            Console.SetCursorPosition(i, 0);
                            Console.Write("■");

                            Console.SetCursorPosition(i, height - 6);
                            Console.Write("■");

                            Console.SetCursorPosition(i, height - 1);
                            Console.Write("■");
                        }
                        //绘制y轴方向边界
                        for (int i = 0; i < height; i++)
                        {
                            Console.SetCursorPosition(0, i);
                            Console.Write("■");

                            Console.SetCursorPosition(width - 2, i);
                            Console.Write("■");
                        }

                        #endregion

                        #region boss属性
                        int bossX = width / 2;
                        int bossY = height / 2;
                        int bossAtkMin = 7;
                        int bossAtkMax = 13;
                        int bossHp = 100;
                        char bossIcon = '■';
                        ConsoleColor bossColor = ConsoleColor.Green;    //使用系统自定义的颜色变量声明boss图标颜色
                        #endregion

                        #region 公主信息
                        int princessX = width / 2;
                        int princessY = height / 2;
                        ConsoleColor princessColor = ConsoleColor.Magenta;
                        char princessIcon = '▲';
                        #endregion

                        #region 玩家角色属性
                        int playerX = 2;
                        int playerY = 1;
                        int playerAtkMin = 8;
                        int playerAtkMax = 12;
                        int playerHp = 100;
                        char playerIcon = '★';
                        ConsoleColor playerColor = ConsoleColor.Blue;
                        #endregion

                        char playerInput;       //变量声明放在循环外以节省性能
                        //声明isFight标识 表示是否进入战斗状态
                        bool isFight = false;
                        //声明isEnd标识 表示游戏是否结束
                        bool isEnd = false;         

                        while (true)
                        {
                            if (isEnd)
                            {
                                break;
                            }
                            #region 绘制boss图标
                            if (bossHp > 0)
                            {
                                Console.SetCursorPosition(bossX, bossY);
                                Console.ForegroundColor = bossColor;
                                Console.Write(bossIcon);
                            }
                            #endregion
                            else
                            {
                                Console.SetCursorPosition(princessX, princessY);
                                Console.ForegroundColor = princessColor;
                                Console.Write(princessIcon);
                                
                            }
                            //处理战斗状态逻辑
                            if (isFight)
                            {
                                #region 战斗逻辑
                                //判断是否判断场景选择
                                if (isEnd)
                                {
                                    break;
                                }
                                while (true)
                                {
                                    if (bossHp >= 0)
                                    {
                                        //读取玩家指令
                                        playerInput = Console.ReadKey(true).KeyChar;
                                        if (playerInput == 'J' || playerInput == 'j')
                                        {
                                            //玩家攻击boss
                                            Random r = new Random();
                                            int atk = r.Next(playerAtkMin, playerAtkMax + 1);
                                            bossHp -= atk;
                                            Console.SetCursorPosition(2, height - 4);
                                            Console.Write("                                       ");
                                            Console.SetCursorPosition(2, height - 4);
                                            Console.ForegroundColor = bossColor;
                                            Console.Write("boss 受到 {0} 点伤害，剩余 {1} 点HP", atk, bossHp);

                                            //boss攻击玩家
                                            if (playerHp >= 0)
                                            {
                                                atk = r.Next(bossAtkMin, bossAtkMax + 1);
                                                playerHp -= atk;
                                                Console.SetCursorPosition(2, height - 3);
                                                Console.Write("                                    ");
                                                Console.SetCursorPosition(2, height - 3);
                                                Console.ForegroundColor = playerColor;
                                                Console.Write("你 受到 {0} 点伤害，剩余 {1} 点HP", atk, playerHp);
                                            }
                                            else
                                            {
                                                //擦除战斗信息
                                                Console.SetCursorPosition(2, height - 5);
                                                Console.Write("                                  ");
                                                Console.SetCursorPosition(2, height - 4);
                                                Console.Write("                                  ");
                                                Console.SetCursorPosition(2, height - 3);
                                                Console.Write("                                  ");
                                                Console.SetCursorPosition(2, height - 5);
                                                Console.Write("很遗憾，你在与boss的战斗中落败");
                                                Console.SetCursorPosition(2, height - 4);
                                                Console.Write("按J键继续");
                                                playerInput = Console.ReadKey(true).KeyChar;
                                                if (playerInput == 'J' || playerInput == 'j')
                                                {
                                                    nowSceneID = 3;
                                                    isEnd = true;
                                                    break;
                                                }
                                            }

                                        }

                                    }

                                    else
                                    {
                                        //擦除战斗信息
                                        Console.SetCursorPosition(2, height - 5);
                                        Console.Write("                                  ");
                                        Console.SetCursorPosition(2, height - 4);
                                        Console.Write("                                  ");
                                        Console.SetCursorPosition(2, height - 3);
                                        Console.Write("                                  ");
                                        Console.SetCursorPosition(2, height - 5);
                                        Console.ForegroundColor = ConsoleColor.Magenta;
                                        Console.Write("恭喜你击败了boss");
                                        Console.SetCursorPosition(2, height - 4);
                                        Console.Write("去拯救公主吧");
                                        Console.SetCursorPosition(2, height - 3);
                                        Console.Write("按J键继续");
                                        //boss死亡，擦除boss图标
                                        Console.SetCursorPosition(bossX, bossY);
                                        Console.Write("  ");
                                        isFight = false;
                                        break;
                                    }
                                }
                                
                                #endregion
                            }
                            
                            //处理非战斗状态逻辑
                            else
                            {
                                #region 实现玩家移动
                                //绘制玩家图标
                                Console.SetCursorPosition(playerX, playerY);
                                Console.ForegroundColor = playerColor;
                                Console.Write(playerIcon);

                                //读取玩家指令
                                playerInput = Console.ReadKey(true).KeyChar;

                                //擦除原图标
                                Console.SetCursorPosition(playerX, playerY);
                                Console.Write("  ");

                                //实现用WASD进行玩家移动
                                switch (playerInput)
                                {
                                    case 'W':
                                    case 'w':
                                        --playerY;
                                        if (playerY < 1)
                                        {
                                            ++playerY;
                                        }
                                        else if (playerX == bossX && playerY == bossY)
                                        {
                                            ++playerY;
                                        }
                                        break;
                                    case 'A':
                                    case 'a':
                                        playerX -= 2;
                                        if (playerX < 2)
                                        {
                                            playerX += 2;
                                        }
                                        else if (playerX == bossX && playerY == bossY)
                                        {
                                            playerX += 2;
                                        }
                                        break;
                                    case 'S':
                                    case 's':
                                        ++playerY;
                                        if (playerY > height - 7)
                                        {
                                            --playerY;
                                        }
                                        else if (playerX == bossX && playerY == bossY)
                                        {
                                            --playerY;
                                        }
                                        break;
                                    case 'D':
                                    case 'd':
                                        playerX += 2;
                                        if (playerX > width - 4)
                                        {
                                            playerX -= 2;
                                        }
                                        else if (playerX == bossX && playerY == bossY)
                                        {
                                            playerX -= 2;
                                        }
                                        break;
                                    case 'J':
                                    case 'j':
                                        //实现按j进入战斗
                                        if (playerX == bossX && playerY == bossY - 1 ||
                                            playerX == bossX && playerY == bossY + 1 ||
                                            playerX == bossX - 2 && playerY == bossY ||
                                            playerX == bossX + 2 && playerY == bossY)
                                        {
                                            if (bossHp > 0)
                                            {
                                                //打印游戏信息
                                                Console.ForegroundColor = ConsoleColor.Yellow;
                                                Console.SetCursorPosition(2, height - 5);
                                                Console.Write("开始战斗，按J键继续");
                                                Console.SetCursorPosition(2, height - 4);
                                                Console.ForegroundColor = bossColor;
                                                Console.Write("boss 剩余血量{0}", bossHp);
                                                Console.SetCursorPosition(2, height - 3);
                                                Console.ForegroundColor = playerColor;
                                                Console.Write("你 剩余血量{0}", playerHp);
                                                //进入战斗状态
                                                isFight = true;
                                            }
                                            else
                                            {
                                                Console.SetCursorPosition(2, height - 5);
                                                Console.Write("                                  ");
                                                Console.SetCursorPosition(2, height - 4);
                                                Console.Write("                                  ");
                                                Console.SetCursorPosition(2, height - 3);
                                                Console.Write("                                  ");
                                                Console.SetCursorPosition(2, height - 5);
                                                Console.ForegroundColor = ConsoleColor.Magenta;
                                                Console.SetCursorPosition(2, height - 5);
                                                Console.Write("成功营救公主");
                                                Console.SetCursorPosition(2, height - 4);
                                                Console.Write("按J键继续");
                                                playerInput = Console.ReadKey(true).KeyChar;
                                                if (playerInput == 'J' || playerInput == 'j')
                                                {
                                                    nowSceneID = 3;
                                                    isEnd = true;
                                                }
                                            }
                                        }
                                        break;
                                }
                                #endregion
                            }
                            
                        }

                        #endregion
                        break;

                    case 3:
                        Console.Clear();
                        #region 结束场景实现
                        Console.SetCursorPosition(width / 2 - 4, 5);        //实现标题居中（汉字占两个x轴单位）
                        Console.WriteLine("游戏结束");

                        nowSelIndex = 0;
                        while (true)
                        {
                            //定义一个标识 isQuitWhile 用于处理在while中的switch中想要使用break直接退出while循环的情况
                            bool isQuitWhile = false;
                            //根据nowSelIndex的值选择相应文字颜色
                            Console.SetCursorPosition(width / 2 - 4, 9);
                            Console.ForegroundColor = nowSelIndex == 0 ? ConsoleColor.Red : ConsoleColor.White;
                            Console.WriteLine("重新开始");

                            Console.SetCursorPosition(width / 2 - 4, 11);
                            Console.ForegroundColor = nowSelIndex == 1 ? ConsoleColor.Red : ConsoleColor.White;
                            Console.WriteLine("结束游戏");

                            //实现输入WS切换 开始 与 结束 选项 ，输入J确定选项功能
                            char c = Console.ReadKey(true).KeyChar;
                            switch (c)
                            {
                                case 'w':
                                case 'W':
                                    --nowSelIndex;
                                    if (nowSelIndex < 0)
                                    {
                                        nowSelIndex = 1;
                                    }
                                    break;
                                case 's':
                                case 'S':
                                    ++nowSelIndex;
                                    if (nowSelIndex > 1)
                                    {
                                        nowSelIndex = 0;
                                    }
                                    break;
                                case 'j':
                                case 'J':
                                    if (nowSelIndex == 0)
                                    {
                                        nowSceneID = 2;
                                        isQuitWhile = true;
                                        break;
                                    }
                                    else
                                    {
                                        Environment.Exit(0);
                                    }
                                    break;
                            }
                            if (isQuitWhile)
                            {
                                break;
                            }
                        }
                        #endregion
                        break;
                }
            }
            #endregion

        }
    }
}

//变量声明最好放在循环外 节省性能